<template>
  <div class="wrap" id="children2">
    <el-dialog
      title="修改信息"
      :visible.sync="state"
      @close="close"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @open="open"
    >
      <el-form>
        <el-form-item label="设置头像" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            :action="url"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :headers="{mlfastsessionid:token}"
            :on-error="error"
            :on-progress="progress"
            :before-upload="beforeAvatarUpload"
            naem="file"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="设置昵称" :label-width="formLabelWidth">
          <el-input autocomplete="off" v-model="username"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="close(1)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { upfile } from "../utils/interface";
let loading = null;
export default {
  props: {
    userState: {
      type: Boolean,
      default: false
    },
    userInfo:{
      type:Object,
      default:{}
    }
  },
  data() {
    return {
      formLabelWidth: "120px",
      imageUrl: "",
      url: "",
      username: "",
      avatar: "",
      token: "",
    };
  },
  computed: {
    state: {
      get() {
        return this.userState;
      },
      set() {}
    }
  },
  methods: {
    open() {
      console.log(this.userInfo)
      this.username = this.userInfo.username
      let ROOT = "";
      if (process.env.NODE_ENV == "development") {
        ROOT = "http://localhost:8080/Jiebao/user/upload.json";
      } else {
        ROOT = "http://www.jiebaodingdan.com/Jiebao/user/upload.json";
      }
      this.url = ROOT;
      console.log(this.url);
    },
    close(v) {
      if (v) {
        this.$emit("getUser", { username: this.username, avatar: this.avatar });
      } else {
        this.$emit("getUser");
      }
    },
    progress() {
      loading = this.$loading({
        lock: true,
        text: "上传中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
    },
    handleAvatarSuccess(res, file) {
      loading.close();

      this.avatar = "http://47.112.155.227" + res.data;
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    error() {
      loading.close();
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 1;


      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 1MB!");
      }
      return  isLt2M;
    }
  },
  created() {
    this.token = localStorage.getItem("shopPC");
  }
};
</script>
<style lang="scss" scoped>
.el-input {
  max-width: 240px;
}
.el-dialog {
  width: 30% !important;
}
</style>
<style lang="scss">
#children2 .el-dialog {
  width: 30% !important;
  min-width: 400px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  object-fit: cover;
}
.el-icon-plus {
  display: flex !important;
  justify-content: center;
  align-items: center;
}
</style>